package org.dochandler.test.tester;

import static org.junit.Assert.assertEquals;

import org.dochandler.service.domain.Officer;
import org.dochandler.service.domain.constants.Constants;
import org.dochandler.service.domain.mission.MissionRequest;
import org.dochandler.test.service.MissionRequestServiceTest;
import org.dochandler.test.service.core.MissionRequestServiceTestImpl;
import org.esfinge.guardian.context.AuthorizationContext;
import org.esfinge.guardian.context.WrappedObj;
import org.esfinge.guardian.exception.AuthorizationException;
import org.junit.Before;
import org.junit.Test;


public class CommanderTester {
	
	private MissionRequest missionRequest;
	private Officer officer;
	
	@Before
	public void setUp() {
		missionRequest = new MissionRequest();
		officer = new Officer();
	}
	
	
	/**
	 * Test of the Security Policy Id SP02
	 */
	@Test
	public void requestMissionsForCommandersTest() {
		officer.setCommander(true);
		
		WrappedObj<MissionRequest> w1 = AuthorizationContext.wrapAsResourceProp( Constants.DOCUMENT.val(), missionRequest);
		WrappedObj<Officer> w2 = AuthorizationContext.wrapAsSubjectProp(Constants.OFFICER.val(), officer);
		
		MissionRequestServiceTest mrs = AuthorizationContext.guardObject(new MissionRequestServiceTestImpl(officer), new Object[] {officer} , w1, w2);
		
		assertEquals("The result must be true", true, mrs.aServiceForCommanders(missionRequest) );
	}
	
	
	/**
	 * Test of the Security Policy Id SP02
	 */
	@Test(expected=AuthorizationException.class)
	public void requestMissionsForNonCommandersTest() {
		officer.setCommander(false);
		
		WrappedObj<MissionRequest> w1 = AuthorizationContext.wrapAsResourceProp( Constants.DOCUMENT.val(), missionRequest);
		WrappedObj<Officer> w2 = AuthorizationContext.wrapAsSubjectProp(Constants.OFFICER.val(), officer);
		
		MissionRequestServiceTest mrs = AuthorizationContext.guardObject(new MissionRequestServiceTestImpl(officer), new Object[] {officer}, w1, w2);
		mrs.aServiceForCommanders(missionRequest);
	}
	
}